package ThreadPoolnio1.Client;

import com.oracle.xmlns.internal.webservices.jaxws_databinding.SoapBindingParameterStyle;

import javax.print.DocFlavor;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {
    private Socket socket = null;
    private PrintWriter outMsg = null;
    private FileOutputStream outFile = null;
    private InputStream in = null;
    private String ip = null;
    private int port;

    public Client(String ip , int port){
        try {
            this.ip = ip;
            this.port = port;
            socket = new Socket(ip , port);

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public  void sendMsg(String msg){
        try {
            if (msg != null && msg.length() > 0){
                if (outMsg == null)
                    outMsg = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),"GBK"));
                outMsg.println(msg);
                outMsg.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public  void closed(){
        try {
            if(outMsg != null)
                outMsg.close();
            if (outFile != null)
                outFile.close();
            if(in != null)
                in.close();
            if (socket != null)
                socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
